//
//  YPAutoTenderCell.m
//  ChengHuiTong
//
//  Created by xzm on 16/8/5.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPAutoTenderCell.h"
#import "UIView+addLine.h"
#import "YPAtuoDetailViewController.h"
@interface YPAutoTenderCell (){

}

@property (nonatomic,strong)UILabel *bMoneyLabel;
@property (nonatomic,strong)UILabel *eMoneyLabel;
@property (nonatomic,strong)UILabel *limitLabel;
@property (nonatomic,strong)UILabel *bTimeLabel;
@property (nonatomic,strong)UILabel *eTimeLabel;

@end

@implementation YPAutoTenderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = 0;
        
//        CGFloat width = (SCREEN_WIDTH - 24)/4;
        CGFloat height = 66;
        self.contentView.height = height;
        
        self.bMoneyLabel = [self labelWithFontSize:12 frame:CGRectMake(0, 0, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        [self.contentView addSubview:self.bMoneyLabel];
        
        UILabel *middleLable1 = [self labelWithFontSize:12 frame:CGRectMake(0, self.bMoneyLabel.bottom, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        
        middleLable1.text = @"-";
        
        [self.contentView addSubview:middleLable1];
        self.eMoneyLabel = [self labelWithFontSize:12 frame:CGRectMake(0, height*2/3.f, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        
        
        [self.contentView addSubview:self.eMoneyLabel];
        
        self.limitLabel = [self labelWithFontSize:12 frame:CGRectMake(self.bMoneyLabel.right, 0, (SCREEN_WIDTH - 24)*0.2, height)];
        
        [self.contentView addSubview:self.limitLabel];
        
        self.bTimeLabel = [self labelWithFontSize:12 frame:CGRectMake(self.limitLabel.right, 0, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        
        [self.contentView addSubview:self.bTimeLabel];
        
        UILabel *middleLable2 = [self labelWithFontSize:12 frame:CGRectMake(self.bTimeLabel.left, self.bMoneyLabel.bottom, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        middleLable2.text = @"-";
        
        [self.contentView addSubview:middleLable2];
        
        
        self.eTimeLabel = [self labelWithFontSize:12 frame:CGRectMake(self.bTimeLabel.left, middleLable2.bottom, (SCREEN_WIDTH - 24)*0.3, height/3.f)];
        
        [self.contentView addSubview:self.eTimeLabel];
        
        UIButton *goDetailBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.eTimeLabel.right, 0, (SCREEN_WIDTH - 24)*0.2, height)];
        [goDetailBtn setTitleColor:kColor_AppNavColor forState:0];
        [goDetailBtn setTitle:@"查看详情" forState:0];
        
        
        [goDetailBtn addTarget:self action:@selector(goDetailVC) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        goDetailBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        
        
        [self.contentView addSubview:goDetailBtn];

        UIColor *sepColor = ColorWithRGB(220, 220, 220);
        [self.contentView addLineWithFrame:CGRectMake(self.bMoneyLabel.right - 0.5, 0, 0.5, height) color:sepColor];
        [self.contentView addLineWithFrame:CGRectMake(self.limitLabel.right - 0.5, 0, 0.5, height) color:sepColor];
        [self.contentView addLineWithFrame:CGRectMake(self.bTimeLabel.right - 0.5, 0, 0.5, height) color:sepColor];
        
        [self.contentView addLine];
//        [self.contentView addLineWithFrame:CGRectMake(0, 65.5, self.contentView, 0.5) color:sepColor];
    }
    
    return self;
}

- (UILabel *)labelWithFontSize:(CGFloat)fontSize frame:(CGRect)frame{
    
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    
    label.font = [UIFont systemFontOfSize:fontSize];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = UIColorFromString(@"#666666");
    
    return label;
}

- (void)setModel:(autoListModel *)model{
    
    _model = model;
    
   }

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.bMoneyLabel.text=[self getMoneyString:_model.min_account];
    
    self.eMoneyLabel.text=[self getMoneyString:_model.tender_account];
    
    self.limitLabel.text=[NSString stringWithFormat:@"%@%@",_model.timelimit,@"个月"];
    
    [self.limitLabel setAdjustsFontSizeToFitWidth:YES];
    
    self.limitLabel.font = [UIFont systemFontOfSize:MIN(12, self.limitLabel.font.pointSize)];
    
    self.bTimeLabel.text=_model.validity_start;
    
    self.eTimeLabel.text=_model.validity_end;

}
- (void)goDetailVC{
    
   
    
    if (self.goDetail) {
        
        self.goDetail(_model.autoid);
    }
    
}


-(NSString*)getMoneyString:(NSString*)money{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterRoundFloor;
    
    NSString*newApr = [formatter stringFromNumber:[NSNumber numberWithFloat:[money floatValue]]];
    return  [NSString stringWithFormat:@"￥%@",newApr];


    
}
@end
